package com.uplooking.pms.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.uplooking.pms.base.controller.BaseController;
import com.uplooking.pms.common.util.CommonUtil;
import com.uplooking.pms.common.vo.DwzPageBean;
import com.uplooking.pms.common.vo.ZTreeBean;
import com.uplooking.pms.system.entity.Organization;
import com.uplooking.pms.system.entity.Role;
import com.uplooking.pms.system.entity.User;
import com.uplooking.pms.system.service.IOrganizationService;
import com.uplooking.pms.system.service.IRoleService;
import com.uplooking.pms.system.service.IUserService;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author chair
 * @since 2017-12-01
 */

@Controller
@RequestMapping("system/organization")
public class OrganizationController extends BaseController {

	@Autowired
	private IOrganizationService ogn;
	
	@Autowired
	private IRoleService ir;

	@RequestMapping("/list")
	public String list(ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {

		Wrapper wrapper = Condition.create().orderBy("sort", true).orderBy("level", true);

		List<Organization> organizations=ogn.selectList(wrapper);
		
		
		if(!CommonUtil.isBlank(organizations)) {
	
			List<ZTreeBean> ztreeBeans=new ArrayList<>();
			ZTreeBean zTreeBean=null;
			for (Organization organization : organizations) {
				zTreeBean = new ZTreeBean();
				
				zTreeBean.setId(organization.getId());
				zTreeBean.setName(organization.getName());
				zTreeBean.setOpen(CommonUtil.isBlank(organization.getPid())?false:true);
				zTreeBean.setpId(organization.getPid());
				
				zTreeBean.setParent(CommonUtil.isBlank(organization.getPid()));
				ztreeBeans.add(zTreeBean);
				zTreeBean=null;
			}
			map.put("ztreeBeans", JSON.toJSONString(ztreeBeans));
			
		}else {
			map.put("", "[]");
		}

		return "system/organization/list";
	}

	@RequestMapping("/toParentInsert")
	public String toParentInsert(HttpServletRequest request, HttpServletResponse response) {

		return "system/organization/parentInsert";
	}
	
	@RequestMapping("/toChildInsert")
	public String toChildInsert(ModelMap map,HttpServletRequest request, HttpServletResponse response) {

		Wrapper<Organization> wrapper=Condition.create().eq("level", 1);
		List<Organization> parents=ogn.selectList(wrapper);
		map.put("parents", parents);
		return "system/organization/childInsert";
	}

	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(Organization organization, HttpServletRequest request, HttpServletResponse response) {
		Wrapper<Organization> wrapper = Condition.create().eq("name", organization.getName());
		Organization organization2 = ogn.selectOne(wrapper);
		if (!CommonUtil.isBlank(organization2)) {
			return ajaxError("组织名重复");
		}

		if (ogn.insert(organization)) {
			return ajaxSuccess("添加成功");
		} else {
			return ajaxError("添加失败");
		}

	}

	@RequestMapping("/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Organization organization=ogn.selectById(id);
		map.put("record",organization );
		if(CommonUtil.isBlank(organization.getPid())) {
			return "system/organization/parentInsert";
		}else {
			Wrapper<Organization> wrapper=Condition.create().eq("level", 1);
			List<Organization> parents=ogn.selectList(wrapper);
			System.out.println(parents.size());
			map.put("parents", parents);
			return "system/organization/childInsert";
		}
		
	}
	
	

	@RequestMapping("/update")
	@ResponseBody
	public Object update(Organization organization, HttpServletRequest request, HttpServletResponse response) {
		if (ogn.updateAllColumnById(organization)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object update(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (ogn.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/toRole")
	public String toRole(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Organization organization=ogn.selectById(id);
		map.put("record",organization );
		if(organization.getRid()!=null) {
			Role rrole=ir.selectById(organization.getRid());
			
			map.put("rrole", rrole);
		}
		List<Role> roles=ir.selectList(null);
		
		map.put("roles", roles);
		//System.out.println(roles.size());
		return "system/organization/ogRoleinsert";
	}

	@RequestMapping("/insertRole")
	@ResponseBody
	public Object insertRole(String gid,String rid,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println(gid+"         "+rid);
		Organization organization= ogn.selectById(gid);
		organization.setRid(rid);
		
		if (ogn.updateById(organization)) {
			System.out.println("success");
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	
	@RequestMapping("/updateRole")
	@ResponseBody
	public Object updateRole(String gid,String rid, HttpServletRequest request, HttpServletResponse response) {
		Organization organization= ogn.selectById(gid);
		organization.setRid(rid);
		organization.setRid(rid);
		if (ogn.updateById(organization)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
		
		

	}

	
}
